var app = getApp();
const utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    windowWidth: 330,
    heightStyle: 445,
    qrCode: '',   //小程序二维码
    imgUrl: '',   //海报url
  },
  onLoad: function (options) {  
    // 获取屏幕宽度px
    let that = this;
    var windowWidth = wx.getSystemInfoSync().windowWidth*0.92;
    var height = windowWidth*94/69;
    that.setData({
     windowWidth: windowWidth,
     height: height
   })
   if(options.type =='supply'){
     wx.setNavigationBarTitle({title: '云点单二维码'});
     that.getSupplyCode();
   } else{
     that.getTableCode();
   }     
 },

  //保存图片
  saveImage: function () {
    var that = this;
    wx.downloadFile({
      url: that.data.imgUrl,
      success: function (res) {
        let tempFilePath = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(response) {        
            wx.showModal({
              content: '图片已保存到相册，赶紧晒一下吧~',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#333',
              success: function (res) {
                if (res.confirm) { }
              },
              fail: function (res) { }
            })
          },
          fail: function (res) {
            console.log('保存失败',res);
            wx.showToast({
              title: '请删掉小程序再重新授权允许保存至相册',
              icon: 'none',
              duration: 3000
            })
          }
        })

      }
    })
    
  },

  //排队点餐码
  getTableCode: function(){
    let that = this;
    wx.showLoading({title: '加载中...',mask: true,})    
    wx.request({
      url: app.globalData.host + '/api/Restaurant/CreateTableCode?Uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0 && res.data.Data) {
          that.setData({
            imgUrl: res.data.Data
          })     
        }else{
          utils.showModal('加载失败！' + res.data.Message); 
        }
      },
    })  
  },
  //云点单聚合码
  getSupplyCode: function(){
    let that = this;
    wx.showLoading({title: '加载中...',mask: true,})    
    wx.request({
      url: app.globalData.host + '/api/supply/CreatePrintQrCode?version=5&uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0 && res.data.Data) {
          that.setData({
            imgUrl: res.data.Data
          })     
        }else{
          utils.showModal('加载失败！' + res.data.Message);         
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },  

})